package com.ssb.util;

import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;


public class AESUtil {
    private static final String defaultCharset = "UTF-8";
    private static final String KEY_AES = "AES";
    private static final String KEY_MD5 = "MD5";
    private static MessageDigest md5Digest;
    static {
        try {
            md5Digest = MessageDigest.getInstance(KEY_MD5);
        } catch (NoSuchAlgorithmException e) {

        }
    }


    //加密方法使用示例：
    public static void main(String[] args) throws Exception {
        String key = "ABQDKEY";
        String data = "{\n" +
                "    \"jsondata\": {\n" +
                "        \"operateType\": \"2\",\n" +
                "        \"policyNo\": \"611020112322020SSK10000065\",\n" +
                "        \"proposalNo\": \"011020112322020SSK10000068\",\n" +
                "        \"status\": \"3\"\n" +
                "    }\n" +
                "}";
        String encrypted = AESUtil.encrypt(data, key);
        String decrypted = AESUtil.decrypt(encrypted, key);
        System.out.println("加密后的密文\n" + encrypted);
        System.out.println("解密后的报文:\n" + decrypted);
    }
    /**
     * 加密
     *
     * @param data 需要加密的内容
     * @param key 加密密码
     * @return
     */
    public static String encrypt(String data, String key) {
        return doAES(data, key, Cipher.ENCRYPT_MODE);
    }
    /**
     * 解密
     *
     * @param data 待解密内容
     * @param key 解密密钥
     * @return
     */
    public static String decrypt(String data, String key) {
        return doAES(data, key, Cipher.DECRYPT_MODE);
    }


    /**
     * 加解密
     *
     * @param data
     * @param key
     * @param mode
     * @return
     */
    private static String doAES(String data, String key, int mode) {
        try {
            boolean encrypt = mode == Cipher.ENCRYPT_MODE;
            byte[] content;
            if (encrypt) {
                content = data.getBytes(defaultCharset);
            } else {
                content = Base64.decodeBase64(data.getBytes());
            }
            byte[] md5Key = md5Digest.digest(key.getBytes(defaultCharset));
            SecretKeySpec keySpec = new SecretKeySpec(md5Key, KEY_AES);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// 创建密码器
            cipher.init(mode, keySpec, new IvParameterSpec(md5Key));// 初始化
            byte[] result = cipher.doFinal(content);
            if (encrypt) {
                return new String(Base64.encodeBase64(result));
            } else {
                return new String(result, defaultCharset);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return data;
    }

    /**
     * 根据身份证获取生日与性别
     * @param cardCode 身份证
     */
    public static Map<String, String> getCarInfo(String cardCode){
        Map<String, String> map = new HashMap<String, String>();
        char[] num = cardCode.toCharArray();
        String sex = "1";
        String birthdate ="";
        if(num.length ==15) {
            String uyear = "19" + cardCode.substring(6, 8);//年份
            String uyue = cardCode.substring(8, 10);//月份
            String uday=cardCode.substring(10, 12);//日
            String usex = cardCode.substring(14, 15);//用户的性别
            if (Integer.parseInt(usex) % 2 == 0) {
                sex = "2";
            }
            birthdate = uyear+"-"+uyue+"-"+uday;
        } else if(num.length == 18) {
            String year = cardCode.substring(6).substring(0, 4);//得到年份
            String yue = cardCode.substring(10).substring(0, 2);//得到月份
            String day = cardCode.substring(12).substring(0,2);//得到日
            if (Integer.parseInt(cardCode.substring(16).substring(0, 1)) % 2 == 0) {//判断性别
                sex = "2";
            }
            birthdate = year+"-"+yue+"-"+day;
        }
        map.put("birthdate", birthdate);
        map.put("sex", sex);
        return map;
    }
}
